﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace E.Y.E.Jobs
{
    class CommandAdd : ICommand
    {
        private const string MESSAGE_COMMAND_TITLE = "/=======ADD COMMAND=======\\";
        private const string MESSAGE_SUCCESSFUL_ADDED = "The task is successfully added to your database.";
        private const string MESSAGE_UNDO_ADD = "Undo: Task \"{0}\" has been removed from your task list.";
        private const string ERROR_MESSAGE_FAIL_ADDING = "Error occured while we are trying to add your job. We are so sorry.";

        ArrayList OriginalDatabase;
        Task AddingTask;
        String Message;
        bool ErrorFlag;

        public CommandAdd()
        {
            OriginalDatabase = new ArrayList();
            AddingTask = new Task();
            Message = "";
            ErrorFlag = true;
        }

        public bool Execute(ref TaskManager taskDatabase, ref CommandTypeManager CommandTypeDatabase, ref History actionsHistory)
        {
            try
            {
                //Get Original Database before doing any process, for undo purposes
                OriginalDatabase = taskDatabase.GetCurrentList();
                taskDatabase.Add(ref AddingTask);
                actionsHistory.AddNewHistory(this);
                Message = MESSAGE_SUCCESSFUL_ADDED;
                ErrorFlag = false;
                return true;
            }
            catch
            {
                Message = ERROR_MESSAGE_FAIL_ADDING;
                ErrorFlag = true;
                return false;
            }
        }

        public bool Undo(ref TaskManager taskDatabase, ref CommandTypeManager CommandTypeDatabase, ref History actionsHistory)
        {
            try
            {
                bool UndoSuccess = taskDatabase.RestoreList(OriginalDatabase);
                Message = String.Format(MESSAGE_UNDO_ADD, AddingTask.Detail);
                ErrorFlag = false;
                return UndoSuccess;
            }
            catch
            {
                return false;
            }
        }

        public string getCommandEventTitle()
        {
            return MESSAGE_COMMAND_TITLE;
        }

        public void setMessage(string desiredMessage)
        {
            Message = desiredMessage;
        }

        public string getMessage()
        {
            return Message;
        }

        public void SetError(bool desiredBool)
        {
            ErrorFlag = desiredBool;
        }

        public bool GetErrorState()
        {
            return ErrorFlag;
        }

        public InformationPackage ConvertToInfoPackage()
        {
            InformationPackage Result = new InformationPackage();

            if (ErrorFlag)
            {
                Result.CommandType = InformationPackageEnumClass.CommandType.OperationError;
            }
            else
            {
                Result.CommandType = InformationPackageEnumClass.CommandType.Add;
                Result.Task = AddingTask;
            }
            Result.Message = Message;

            return Result;
        }

        public int setEventID
        {
            get
            {
                return AddingTask.ID;
            }
            set
            {
                AddingTask.ID = value;
            }
        }

        public string setEventDetails
        {
            get
            {
                return AddingTask.Detail;
            }
            set
            {
                AddingTask.Detail = value;
            }
        }

        public string setEventVenue
        {
            get
            {
                return AddingTask.Venue;
            }
            set
            {
                AddingTask.Venue = value;
            }
        }

        public DateTime setStartTime
        {
            get
            {
                return AddingTask.StartTime;
            }
            set
            {
                AddingTask.StartTime = value;
            }
        }

        public DateTime setEndTime
        {
            get
            {
                return AddingTask.EndTime;
            }
            set
            {
                AddingTask.EndTime = value;
            }
        }

        public TaskEnumTypes.TaskStatus setEventStatus
        {
            get
            {
                return AddingTask.Status;
            }
            set
            {
                AddingTask.Status = value;
            }
        }

        public TaskEnumTypes.TaskPriority setPriority
        {
            get
            {
                return AddingTask.Priority;
            }
            set
            {
                AddingTask.Priority = value;
            }
        }

        public TaskEnumTypes.ReminderType setReminderType
        {
            get
            {
                return AddingTask.TypeOfReminder;
            }
            set
            {
                AddingTask.TypeOfReminder = value;
            }
        }

        public TimeSpan setRemindTimeBefore
        {
            get
            {
                return AddingTask.RemindBefore;
            }
            set
            {
                AddingTask.RemindBefore = value;
            }
        }
    }
}
